﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace _2DGame
{
    class Network
    {
        private static LinkedList<Message> QueueMessage = new LinkedList<Message>();
        static bool Checking = false;
        static int sec;
        /// <summary>
        /// This method will send the data over the internet to the server were the server will pick it upp and store it in it's database.
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Mail"></param>
        /// <param name="Tele"></param>
        /// <param name="Adress"></param>
        /// <param name="Score"></param>
        public static void client(string Name, string Mail, string Tele, string Adress, long Score)
        {
            try
            {
                IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 673);
                Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                clientSocket.ReceiveTimeout = 1000;
                clientSocket.SendTimeout = 1000;
                try
                {
                    clientSocket.Connect(ip);
                }
                catch
                {
                    Queues(Name, Mail, Tele, Adress, Score);
                    return;
                }
                string Received = "";
                while (Received != "Message Received")
                {
                    //needs to wait after each send to make sure the server received the message.
                    clientSocket.Send(Encoding.ASCII.GetBytes(Name));
                    Thread.Sleep(10);
                    clientSocket.Send(Encoding.ASCII.GetBytes(Mail));
                    Thread.Sleep(10);
                    clientSocket.Send(Encoding.ASCII.GetBytes(Tele));
                    Thread.Sleep(10);
                    clientSocket.Send(Encoding.ASCII.GetBytes(Adress));
                    Thread.Sleep(10);
                    clientSocket.Send(Encoding.ASCII.GetBytes(Convert.ToString(Score)));
                    byte[] data = new byte[1024];
                    //need something to prevent the code from getting stuck on this line.
                    Thread sec = new Thread(new ThreadStart(CountSec));
                    sec.Start();
                    int receivedDataLength = clientSocket.Receive(data);
                    Received = System.Text.Encoding.ASCII.GetString(data, 0, receivedDataLength);
                }
                clientSocket.Shutdown(SocketShutdown.Both);
                clientSocket.Close();
                Console.Write("Client end");
            }
            catch
            {
                Thread.Sleep(100);
                client(Name, Mail, Tele, Adress, Score);
            }
        }
        private static void CountSec()
        {
            sec += 1;
            if (sec == 5)
            {

            }
            Thread.Sleep(1000);
        }
        private static void Queues(string Name, string Mail, string Tele, string Adress, long Score)
        {
            Message message = new Message();
            message.Name = Name;
            message.Mail = Mail;
            message.Tele = Tele;
            message.Adress = Adress;
            message.Score = Score;
            QueueMessage.AddFirst(message);
            if (Checking == false)
            {
                Checking = true;
                Thread ConnectionChecker = new Thread(new ThreadStart(CheckForConnection));
                ConnectionChecker.Start();
            }

        }
        private static void CheckForConnection()
        {
            IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 673);
            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                clientSocket.Connect(ip);
                clientSocket.Shutdown(SocketShutdown.Both);
                clientSocket.Close();
                for(int i = 0; i < QueueMessage.Count; i++)
                {
                    client(QueueMessage.ElementAt(i).Name, QueueMessage.ElementAt(i).Mail, QueueMessage.ElementAt(i).Tele, QueueMessage.ElementAt(i).Adress, QueueMessage.ElementAt(i).Score);
                }
            }
            catch
            {
                CheckForConnection();
                //sleeps for 1 min.
                Thread.Sleep(60000);
            }
        }
    }
    
}
